﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace zielonka.co.uk.extensions.system.data
{
    // Description: Use the Get[Type] functions that are part of the IDataReader but by passing the field name as a string as opposed to the field ordinal as int. Allows assigning default values for null values returned by the datareader.
    /*
        var itemID = datareader.GetInt32("ItemID");

        //With Default

        var ItemName = datareader.GetString("ItemName", "Unknown");
    */


    // A set of extension methods for IDataReader
    public static partial class DataReaderExtensions
    {

        /// <summary>
        /// This method extends the GetInt16 method of the data reader to allow calling by the field name
        /// </summary>
        /// <param name="dataReader">The datareader object we are extending</param>
        /// <param name="fieldName">The field name that we are getting the Int16 value for</param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Int16 GetInt16(this IDataReader dataReader, string fieldName, Int16 defaultValue)
        {
            var fieldOrdinal = dataReader.GetOrdinal(fieldName);
            return dataReader.IsDBNull(fieldOrdinal) ? defaultValue : dataReader.GetInt16(fieldOrdinal);
        }

    }
}
